FreeMem - invalid pointer operation

Otázka od: Roland Turcan

6. 11. 2002 0:01

Hello delphi-l@clexpert.cz!

var
  FS:TFileStream;
  Source:string;
  Buffer:PChar;
...
GetMem(Buffer,Length(Source));
try
  Buffer:=PChar(MyFunction(Source));
  FS.Position:=0;
  FS.WriteBuffer(Buffer^,Length(Buffer));
finally
  FreeMem(Buffer); /// tento riadok sposobuje exception
end;


v helpe je priklad:

GetMem(Buffer, Size);
try
  BlockRead(F, Buffer^, Size);
  ProcessFile(Buffer, Size);
finally
  FreeMem(Buffer);
end;

Podla mna je to to iste ako v helpe. :-/
--
Best regards, TRoland
http://www.rotursoft.sk

Odpovedá: Petr Vones

6. 11. 2002 0:11

From: "Roland Turcan" <rolo@sedas.sk>
> var
> FS:TFileStream;
> Source:string;
> Buffer:PChar;
> ...
> GetMem(Buffer,Length(Source));
> try
> Buffer:=PChar(MyFunction(Source));
> FS.Position:=0;
> FS.WriteBuffer(Buffer^,Length(Buffer));
> finally
> FreeMem(Buffer); /// tento riadok sposobuje exception

Jiste, protoze nejdrive alokoujes pamet a pointer ukladas do promenne Buffer,
potom do te same promenne ukladas pointer na ten string a na konci se snazis
pamet patrici te string promenne uvolnit. Pokud chces ulozit string do streamu
tak to udelej jednoduse:

Source: string;

FS.WriteBuffer(Pointer(Source)^, Length(Source));

Petr Vones